<?php

class Content_Page_Main_Exception extends Page_Main_Exception {}
class Content_Page_Main_Invalid_Exception extends Content_Page_Main_Exception {}

abstract class Content_Page_Main extends Page_Main {

	protected function URL($array = array()){
		return parent::URL(array_merge(
			array('content'),
			$array
		));
	}

	public function rid($array = array()){
		return array_merge(
			array('content'),
			$array
		);
	}

	/** Load a Page Content **/	

	public static function Load($args){
		$user = Authentication::getInstance()->user();

		$perms = ACL::GetUser($user, "content",array(
				'v' => 'View',
				'e' => 'Edit',
				'a' => 'Add',
				'd' => 'Delete'
			));

		if($args['parent']->data_top() == 'add' && isset($perms['a'])){
			// Add Page
			CMS::File('content/page_main/add');
			return Page_Content_Add::Load($data, $rewrite, $parent);
		}

		if(((int) $args['parent']->data_top()) != 0){
			// We have a Content!
			$args['id'] = $args['parent']->data_top();

			$args['parent']->data_shift();

			$perms = ACL::GetUser($user, "content/{$args['id']}",array(
					'v' => 'View',
					'e' => 'Edit',
					'a' => 'Add',
					'd' => 'Delete'
				));

			if($args['parent']->data_top() == 'edit' && isset($perms['e'])){
				// Edit
				$args['parent']->data_shift();
				CMS::File('content/page_main/edit');
				return Content_Page_Main_Edit::Load($args);
			}			
			if($args['parent']->data_top() == 'delete' && isset($perms['d'])){
				// Delete
				$args['parent']->data_shift();
				CMS::File('content/page_main/delete');
				return Page_Content_Delete::Load($args);
			}			
			if($args['parent']->data_top() == 'view' && isset($perms['v'])){
				// View
				$args['parent']->data_shift();
				CMS::File('content/page_main/view');
				return Content_Page_Main_View::Load($args);
			}

			if(isset($perms['v'])){
				CMS::File('content/page_main/view');
				return Content_Page_Main_View::Load($args);
			}
		}
		throw new Content_Page_Main_Invalid_Exception($d);
	}

	public function output($child = null){
		$template = Output::getInstance()->Template(array('content','page'));
		if(isset($child)){
			$template->mode = $child;
		}
		return parent::output($template);
	}

}

